package com.epoint.mobileoa.tools.controls.tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ProgressDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.service.MainService;

/**
 * @author liyc
 * @time 2012-4-14 下午1:44:22
 * @annotation 代码项树控件
 */
public class Tree extends SuperView {

	private ListView lv;
	
	private ProgressDialog m_Dialog;

	private String treetitle,itemname,rootitemvalue,style;
	
	public static List<Map<String,String>> chooseNodeData = new ArrayList<Map<String,String>>();
	
	private boolean unleafchoose;
	
	private int type;//单多选
	
	@Override
	public void initControls() {
		lv = (ListView) findViewById(R.id.code_list);
		lv.setOnItemClickListener(this);
		top_right_bt.setVisibility(View.VISIBLE);
		top_right_bt.setOnClickListener(this);
		top_right_bt.setText("确定");

		treetitle = getIntent().getStringExtra("treetitle");
		itemname = getIntent().getStringExtra("itemname");
		rootitemvalue = getIntent().getStringExtra("rootitemvalue");
		unleafchoose = getIntent().getBooleanExtra("unleafchoose", false);
		style = getIntent().getStringExtra("style");
		type = getIntent().getIntExtra("type", 1);
		
		if(treetitle==null){
			finish();
			return;
		}

		else{
			top_title.setText(treetitle);
			showProgressDialog();
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put("rootitemvalue", rootitemvalue);
			params.put("ItemName", itemname);
			params.put("style", style);
			params.put("unleafchoose", unleafchoose);
			params.put("type", type);
			Task task = new GetTreeDataTask(this, params);
			MainService.addTask(task);
		}
	}

	private void showProgressDialog(){
		m_Dialog = new ProgressDialog(this);
		m_Dialog.setMessage("正在加载数据...");	
		m_Dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		m_Dialog.setCancelable(true);
		m_Dialog.show();
	}

	@Override
	public void refresh(Object... param) {
		if(param[0].toString().equals(MainService.NORMAL)){
			lv.setAdapter((ListAdapter) param[1]);
		}
		m_Dialog.dismiss();
	}

	@Override
	public int getLayoutInt() {
		return R.layout.tree;
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		((TreeAdapter)parent.getAdapter()).ExpandOrCollapse(position);
	}
	
	@Override
	public void onClick(View v) {
		if(v==top_right_bt){
			chooseNodeData.clear();
			
			Map<String,String> map;
			
			List<Node> nodes = ((TreeAdapter)lv.getAdapter()).getSeletedNodes();

			for(int i=0;i<nodes.size();i++){
				
				Node n = nodes.get(i);
				
				boolean isleaf = true;
				
				/*if(!unleafchoose){
					isleaf = n.isLeaf();
				}*/
				
				if(isleaf){
					map = new HashMap<String, String>();
					map.put("text", n.getText());
					map.put("param1", n.getParam1());
					map.put("param2", n.getParam2());
					chooseNodeData.add(map);
				}
				
			}
			finish();
		}
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param){
	}

}
